package com.linkoog.devtools.middleware.toolwindow;

import com.intellij.ide.startup.StartupManagerEx;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowEx;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.impl.ContentImpl;
import com.intellij.util.ModalityUiUtil;
import com.linkoog.devtools.middleware.toolwindow.dashboard.MiddlewareDashboardUI;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;

/**
 *    workbenche  工作台
 *
 *    dashboard   仪表盘
 *
 *    integration  集成
 */
public class MiddlewareWindowManager implements Disposable {

    String TOOL_WINDOW_ID = "com.linkoog.devtools.Middleware";
    String TOOL_WINDOW_NAME = "Middleware Manager";
    private final Project project;

    public MiddlewareWindowManager(@NotNull Project project) {
        this.project = project;
    }


    @Override
    public void dispose() {
    }

    public @NotNull ToolWindowEx getOrCreateToolWindow() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ToolWindowEx toolWindowEx = (ToolWindowEx)toolWindowManager.getToolWindow(TOOL_WINDOW_ID);
        if (toolWindowEx != null) {
            return toolWindowEx;
        }

        toolWindowEx = (ToolWindowEx)toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, builder -> {
            builder.stripeTitle = () -> TOOL_WINDOW_NAME;
            builder.icon = IconLoader.getIcon("/icons/middleware/services.svg", MiddlewareWindowManager.class);
            builder.canCloseContent = true;
            builder.hideOnEmptyContent = true;
            builder.anchor = ToolWindowAnchor.BOTTOM;
            builder.sideTool = false;
            return Unit.INSTANCE;
        });
        toolWindowEx.setAutoHide(true);

        MiddlewareDashboardUI middlewareDashboardUI = new MiddlewareDashboardUI(project);
        ContentImpl content = new ContentImpl(middlewareDashboardUI, "Dashboard", false);
        content.setCloseable(false);
        Disposer.register(content, middlewareDashboardUI);

        ContentManager contentManager = toolWindowEx.getContentManager();
        contentManager.addContent(content);
        contentManager.setSelectedContent(content);

        return toolWindowEx;
    }

    public void show(){
        invokeLaterIfNeeded(() -> {
            ToolWindowEx toolWindowEx = getOrCreateToolWindow();
            toolWindowEx.show();
        });
    }



    public void removeContent(Content content) {
        invokeLaterIfNeeded(() -> {
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID);
            ContentManager contentManager = toolWindow == null ? null : toolWindow.getContentManager();
            if (contentManager != null && !contentManager.isDisposed()) {
                contentManager.removeContent(content, true);
            }
        });
    }

    private void invokeLaterIfNeeded(@NotNull Runnable runnable) {
        if (project.isDefault()) {
            return;
        }

        StartupManagerEx.getInstanceEx(project).runAfterOpened(() -> {
            ModalityUiUtil.invokeLaterIfNeeded(ModalityState.defaultModalityState(), project.getDisposed(), runnable);
        });
    }






}
